const { User } = require("../models");

class UserController {
  /**
   * 用户注册
   */
  static async register(req, res) {
    const { firstName, email } = req.body;

    if (!firstName || !email) {
      return res.status(400).json({
        success: false,
        message: "firstName and email are required",
      });
    }

    try {
      // 创建用户
      const newUser = await User.create({ firstName, email });
      res.status(201).json({
        success: true,
        message: "User registered successfully",
        data: newUser,
      });
    } catch (error) {
      console.error("Error registering user:", error);
      res.status(500).json({
        success: false,
        message: "Failed to register user",
        error: error.message,
      });
    }
  }

  /**
   * 通过 firstName 查询用户详情
   */
  static async getUserByFirstName(req, res) {
    const { firstName } = req.params;

    if (!firstName) {
      return res.status(400).json({
        success: false,
        message: "firstName is required",
      });
    }

    try {
      // 查询用户
      const user = await User.findOne({
        where: { firstName },
      });

      if (!user) {
        return res.status(404).json({
          success: false,
          message: "User not found",
        });
      }

      res.status(200).json({
        success: true,
        message: "User found",
        data: user,
      });
    } catch (error) {
      console.error("Error fetching user:", error);
      res.status(500).json({
        success: false,
        message: "Failed to fetch user",
        error: error.message,
      });
    }
  }

  /**
   * 更新用户信息
   */
  static async updateUser(req, res) {
    const { id } = req.params;
    const { firstName, email } = req.body;

    if (!id || (!firstName && !email)) {
      return res.status(400).json({
        success: false,
        message: "id and at least one field (firstName or email) are required",
      });
    }

    try {
      // 查找用户
      const user = await User.findByPk(id);

      if (!user) {
        return res.status(404).json({
          success: false,
          message: "User not found",
        });
      }

      // 更新用户信息
      if (firstName) user.firstName = firstName;
      if (email) user.email = email;
      await user.save();

      res.status(200).json({
        success: true,
        message: "User updated successfully",
        data: user,
      });
    } catch (error) {
      console.error("Error updating user:", error);
      res.status(500).json({
        success: false,
        message: "Failed to update user",
        error: error.message,
      });
    }
  }

  /**
   * 删除用户
   */
  static async deleteUser(req, res) {
    const { id } = req.params;

    if (!id) {
      return res.status(400).json({
        success: false,
        message: "id is required",
      });
    }

    try {
      // 查找用户
      const user = await User.findByPk(id);

      if (!user) {
        return res.status(404).json({
          success: false,
          message: "User not found",
        });
      }

      // 删除用户
      await user.destroy();

      res.status(200).json({
        success: true,
        message: "User deleted successfully",
      });
    } catch (error) {
      console.error("Error deleting user:", error);
      res.status(500).json({
        success: false,
        message: "Failed to delete user",
        error: error.message,
      });
    }
  }
}

module.exports = UserController;
